Skip to content

fix(mcp): clear backendPromise in onClose to allow backend recreation#40019

Closed
mlugo-apx wants to merge 2 commits intomicrosoft:mainfrom
mlugo-apx:fix/mcp-clear-backend-promise-on-close
Closed

fix(mcp): clear backendPromise in onClose to allow backend recreation#40019
mlugo-apx wants to merge 2 commits intomicrosoft:mainfrom
mlugo-apx:fix/mcp-clear-backend-promise-on-close

Conversation

@mlugo-apx
Copy link
Copy Markdown

Summary

Fixes #40018

When onClose fires, the handler was disposing the backend but not clearing backendPromise. This caused subsequent tool calls to await the stale promise and receive a disposed backend, resulting in "Not connected" errors.

Changes

Before:

const onClose = () => backendPromise?.then(b => backendManager.disposeBackend(b)).catch(serverDebug);

After:

const onClose = () => {
  const p = backendPromise;
  backendPromise = undefined;
  p?.then(b => backendManager.disposeBackend(b)).catch(serverDebug);
};

Testing

  • Tested locally for 6+ hours with instrumentation logging
  • Connection remained stable throughout
  • Fix allows automatic backend recreation after any disconnect event

mlugo-apx and others added 2 commits April 1, 2026 22:36
When onClose fires, the handler was disposing the backend but not clearing
backendPromise. This caused subsequent tool calls to await the stale promise
and receive a disposed backend, resulting in 'Not connected' errors.

The fix clears backendPromise before disposing, allowing the next tool call
to create a fresh backend.

Fixes microsoft#40018
- Add browserConnectionEnded flag to track unexpected disconnections
- Prepend recovery notice to tool result when reconnecting between calls
- Use wording 'state might be lost' per maintainer feedback
- Only show notice for disconnects between tool calls, not during
@pavelfeldman
Copy link
Copy Markdown
Member

Unclear what you are testing without a test. This onClose is from server.onClose, server is gone. backendPromise is in the scope of creating that server, irrelevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MCP] backendPromise not cleared in onClose causes stale backend after disconnect

2 participants